package pkg.action;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import net.sourceforge.stripes.action.After;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.validation.ValidationErrors;
import pkg.dao.EntityDAO;
import pkg.dao.PagedList;
import pkg.entity.Grade;
import pkg.filter.GradeFilter;

public class GradeActionBean extends BaseActionBean {

    private PagedList<Grade> grades;
    private List<Grade> gradeList;
    private List<Long> gradeIds;
    private Integer page;
    private String sort;
    private String dir;
    private String mode;
    private String filterText;
    private Grade grade;

    public GradeActionBean() {
    }

    @DefaultHandler
    public Resolution view() {
        loadGrades();
        return new ForwardResolution("/GradeList.jsp");
    }

    public void loadGrades() {
        EntityDAO sb;

        sb = new EntityDAO();
        //TODO
        GradeFilter f = new GradeFilter();
        filterText = (String) getSession().getAttribute("filterText");
        if (filterText != null) {
            f.setSimple(filterText);
        }
        gradeList = sb.findAllByFilter(f);
        grades = new PagedList<Grade>();
        grades.setResults(sb.findAllByFilter(f));
        if (page == null) {
            page = 1;
        }
        grades.setPageNumber(page);
    }

    public Resolution addNew() {
        return new RedirectResolution("/Grade.action?create");
    }

    public Resolution deleteSelected() {
        EntityDAO sb = new EntityDAO();

        if (getGradeIds() != null) {
            sb.delete(Grade.class, gradeIds);
        }
        loadGrades();
        setCache("teacherMap", null);
        setCache("teacherTime", new Date());
        RedirectResolution result;
        result = new RedirectResolution(this.getClass());
        result.addParameter("page", page);
        return result;
    }

    public Resolution preEdit() {
        EntityDAO sb;
        sb = new EntityDAO();
        grade = sb.find(Grade.class, grade.getId());
        loadLookups();
        return new ForwardResolution("/GradeEdit.jsp");
    }

    public Resolution save() throws IOException {
        EntityDAO sb = new EntityDAO();
        Grade origItem = null;

        if (grade.getId() != null) {
            origItem = sb.find(Grade.class, grade.getId());
        }

//            if (origItem == null) {
//                ValidationError error = new SimpleError("Item code is invalid.");
//                getContext().getValidationErrors().add("itemCode", error);
//                return getContext().getSourcePageResolution();
//            }

        // Link up parent to children
        sb.update(grade);
        setCache("teacherMap", null);
        setCache("teacherTime", new Date());
        RedirectResolution result;
        result = new RedirectResolution(this.getClass());
        result.addParameter("page", page);
        return result;
    }

    @After(stages = LifecycleStage.BindingAndValidation)
    public void loadRequestIfErrors() {
        ValidationErrors ve = getContext().getValidationErrors();
        if (ve.size() > 0 && grade != null && grade.getId() != null) {
            EntityDAO sb = new EntityDAO();
            grade = sb.find(Grade.class, grade.getId());
        }
        loadLookups();
    }

    public Resolution create() {
        grade = new Grade();
        mode = "add";
        loadLookups();
        return new ForwardResolution("/GradeEdit.jsp");
    }

    public Resolution applyFilter() {
        if (filterText != null) {
            getSession().setAttribute("filterText", filterText);
        } else {
            getSession().removeAttribute("filterText");
        }
        return new RedirectResolution(this.getClass());
    }

    public void loadLookups() {
        EntityDAO sb = new EntityDAO();
        // TODO
    }

    //<editor-fold defaultstate="collapsed" desc=" Setters/Getters ">
    public Grade getGrade() {
        return grade;
    }

    public void setGrade(Grade category) {
        this.grade = category;
    }

    public Grade getItem() {
        return grade;
    }

    public void setItem(Grade item) {
        this.grade = item;
    }

    public PagedList<Grade> getGrades() {
        return grades;
    }

    public void setGrades(PagedList<Grade> items) {
        this.grades = items;
    }

    public List<Grade> getGradeList() {
        return gradeList;
    }

    public void setGradeList(List<Grade> itemList) {
        this.gradeList = itemList;
    }

    public List<Long> getGradeIds() {
        return gradeIds;
    }

    public void setGradeIds(List<Long> GradeIds) {
        this.gradeIds = GradeIds;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public String getDir() {
        return dir;
    }

    public void setDir(String dir) {
        this.dir = dir;
    }

    public String getMode() {
        return mode;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    public String getFilterText() {
        return filterText;
    }

    public void setFilterText(String filterText) {
        this.filterText = filterText;
    }
    //</editor-fold>
}
